﻿<%@ WebHandler Language="C#" Class="FileUpload" %>

using System;
using System.Web;
using System.Web.UI;

using LMD.Constant;
using LMD.Lib.Interface;
using LMD.Lib.Controls;


public class FileUpload : IHttpHandler {
    
    String m_sxOutput = String.Empty;
    
    public void ProcessRequest (HttpContext ctx)
    {
        String sxHandlerName = ctx.ReadQuery("handlername");        
        
        String sxAction = ctx.ReadQuery("action");

        if (String.IsNullOrEmpty(sxAction))
        {
            ctx.Response.Write("No Action specified!!!");
            return;
        }
        
        String sxControlId = ctx.ReadQuery("cid");
        
        if (String.IsNullOrEmpty(sxControlId))
        {
            ctx.Response.Write("No Control ID specified!!!");
            return;
        }
        
        String sxValue = ctx.ReadQuery("value");
        int iDataTypeId = ctx.ReadInt("datatype"); // KALO DI LOV, ini adalah selected otf combo (tipe info)

        switch (sxAction.ToUpper())
        {
            case KFileHandler.Action.UPLOAD:
                doUpload(ctx, sxHandlerName, sxControlId, sxValue);
                break;
            case KFileHandler.Action.DOWNLOAD:
                doDownload(sxValue);
                return;
            case KFileHandler.Action.DELETE:
                doDelete(sxHandlerName, sxControlId, sxValue, iDataTypeId);
                break;
        }
        
        ctx.Response.ContentType = "text/plain";        
        ctx.Response.Write(m_sxOutput);
    }

    private static void doDownload(String sxFileId)
    {
        FileHandler.DownloadFile(sxFileId);
    }

    private void doDelete(String sxHandlerName, String sxControlId, String sxFileId, int iDataTypeId)
    {
        FileHandler.DeleteFile(sxFileId);


        if (String.IsNullOrEmpty(sxHandlerName))
        {
            HttpContext.Current.Response.Write("No Handler name specified!!!");
            return;
        } 
        
        ILOVProvider lov = LOVProvider.getProvider(sxHandlerName);

        if (lov == null)
        {
            TextboxType tpTextbox = CustomControls.GetTextBoxType(sxHandlerName, null);
            String sxUploader = String.Empty;
            if (sxControlId.StartsWith("CD"))
            {
                sxUploader = CustomControls.BuildEditor_FileUpload(sxControlId, sxHandlerName, null,
                KControl.INPUT_ID + "_" + sxControlId, tpTextbox);
            }
            else
            {
                sxUploader = CustomControls.BuildEditor_FileUpload(sxControlId, sxHandlerName, null,
                    KControl.INPUT_ID, tpTextbox);
            }
            m_sxOutput = KGlobal.DELIMITER + ": " + sxUploader;
        }
        else
        {
            m_sxOutput = 
                iDataTypeId.ToString() + KGlobal.DELIMITER + ": " + 
                CustomControls.Create_TextBox(sxControlId, KControl.INPUT_ID, null, true, (TextboxType)iDataTypeId);
        }
    }

    private void doUpload(HttpContext ctx, String sxHandlerName, String sxControlId, String sxValue)
    {
        if (String.IsNullOrEmpty(sxHandlerName))
        {
            HttpContext.Current.Response.Write("No Handler name specified!!!");
            return;
        }
        String[] sxKeys = ctx.Request.Files.AllKeys;
        foreach (String sxKey in sxKeys)
        {
            if (sxKey.Length > 3)
            {
                m_sxOutput = FileHandler.UploadFile(sxControlId, sxValue, sxHandlerName,
                    ctx.Request.Files[KControl.INPUT_ID + "_" + sxControlId]);
            }
            else
            {
                m_sxOutput = FileHandler.UploadFile(sxControlId, sxValue, sxHandlerName,
                    ctx.Request.Files[KControl.INPUT_ID]);
            }
        }
        //if (ctx.Request.Files.Count > 1)
        //{
        //    m_sxOutput = FileHandler.UploadFile(sxControlId, sxValue, sxHandlerName,
        //    ctx.Request.Files[KControl.INPUT_ID + "_" + sxControlId]);
        //}
        //else
        //{
        //    m_sxOutput = FileHandler.UploadFile(sxControlId, sxValue, sxHandlerName,
        //        ctx.Request.Files[KControl.INPUT_ID]);
        //    //ctx.Request.Files[KControl.INPUT_ID + "_" + sxControlId]);
        //}
    }
    
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    

}